//
//  CustomUIView.m
//  UI05_触摸_手势
//
//  Created by aaaaaa on 16/6/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CustomUIView.h"

@interface CustomUIView ()

@property (nonatomic, assign) CGPoint labPoint;

@end


@implementation CustomUIView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

/*
 * 下面四个方法是UIResponder类的方法
 * 子类在重写这些方法是要调用父类方法
 */

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    //获取集合中的UITouch对象
    UITouch *touch = [touches anyObject];
    
    /*
     *locationInview参数
     *  触摸点是相对于哪个视图来说的
     */
    //获取点击的坐标
    CGPoint startPoint = [touch locationInView:self.superview];
    NSLog(@"x : %lf  y : %lf", startPoint.x, startPoint.y);
   
    self.labPoint = CGPointMake(startPoint.x - self.center.x, startPoint.y - self.center.y);
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [super touchesMoved:touches withEvent:event];
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    NSLog(@"%@", touches);
    NSLog(@"%ld", touches.count);
    
    UITouch *touch = [touches anyObject];
    CGPoint movePoint = [touch locationInView:self.superview];
    NSLog(@"x: %lf  y : %lf", movePoint.x, movePoint.y);
    
    self.center = CGPointMake(movePoint.x - self.labPoint.x, movePoint.y - self.labPoint.y);

}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesCancelled:touches withEvent:event];
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //当正在触摸时，来电话会取消触摸事件，并调用此方法
    [super touchesEnded:touches withEvent:event];
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
}


@end
